{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Neeps - Easy\n",
    "\n",
    "The \"Neeps\" database includes details of all teaching events in the School of Computing at Napier University in Semester 1 of the 2000/2001 academic year.\n",
    "\n",
    "Graduated questions\n",
    "\n",
    "The timetable database contains the following tables:\n",
    "\n",
    "- ut_staff\n",
    "\n",
    "id | name\n",
    "---|-----\n",
    "co.ACg | Cumming, Andrew\n",
    "co.ACr | Crerar, Dr Alison\n",
    "co.AFA | Armitage, Dr Alistair\n",
    " | ... Result truncated.\n",
    "\n",
    "The staff table simply gives the unique identifier for each member of staff together with their full name.\n",
    "\n",
    "- ut_student\n",
    "\n",
    "id | name | sze | parent\n",
    "---|------|----:|----------\n",
    "co1.BAe | BAe | 15 | \n",
    "co1.CO | Computing 1st Year | 150 | \n",
    "co1.CO.a | Computing 1st Year a | 20 | co1.CO\n",
    "co1.CO.c | Computing 1st Year c | 20 | co1.CO\n",
    "co1.CO.d | Computing 1st Year d | 20 | co1.CO\n",
    " | | | ... Result truncated.\n",
    "\n",
    "Each entry in the student table represents a group of students who follow the same timetable. The parent field is used to maintain a hierachy of groups. A student in group co1.CO.a is also in group co1.CO\n",
    "\n",
    "- ut_room\n",
    "\n",
    "id | name | capacity | parent\n",
    "---|------|---------:|---------\n",
    "co.117+118 |  | 32 | \n",
    "co.117 |  | 16 | co.117+118\n",
    "co.118 |  | 16 | co.117+118\n",
    "co.B7 |  | 40 | \n",
    " | | | ... result truncated\n",
    "\n",
    "All classrooms are listed in the room table. The parent relation is used to maintain groupings. The row co.117+118 represents a pair of rooms - sometimes these rooms are used for a single event.\n",
    "\n",
    "- ut_event\n",
    "\n",
    "id | modle | kind | dow | tod | duration | room\n",
    "---|-------|------|-----|-----|---------:|----------\n",
    "co12004.L01 | co12004 | L | Wednesday | 11:00 | 1 | cr.SMH\n",
    "co12004.L02 | co12004 | L | Monday | 17:00 | 1 | cr.B13\n",
    "co12004.T01 | co12004 | T | Monday | 11:00 | 2 | co.G78+G82\n",
    "co12004.T02 | co12004 | T | Tuesday | 15:00 | 2 | co.B7\n",
    "co12004.T03 | co12004 | T | Tuesday | 13:00 | 2 | co.G78+G82\n",
    " | | | | | | ... Result truncated.\n",
    "\n",
    "Every event is associated with a single modle (deliberately mis-spelled version of module - which is a reserved word in one of engines). An event occurs on a particular day of the week (dow) at a particular time of day (tod). Every event has a duration specified as a whole number of hours.\n",
    "\n",
    "- ut_attends\n",
    "\n",
    "student | event\n",
    "--------|------\n",
    "co1.BAe | co12004.L01\n",
    "co1.CO | co12004.L01\n",
    "co1.IS | co12004.L01\n",
    "co1.SE.pt | co12004.L02\n",
    "co1.CO.a | co12004.T01\n",
    " | ... Result truncated.\n",
    "\n",
    "The attends table links event to student. It realises the many to many relation.\n",
    "\n",
    "- ut_teaches\n",
    "\n",
    "staff | event\n",
    "------|----------\n",
    "co.AMn | co12004.L01\n",
    "co.RK | co12004.L01\n",
    "co.SRM | co12004.L01\n",
    "co.RK | co12004.L02\n",
    "co.SRM | co12004.L02\n",
    " | ... Result truncated.\n",
    "\n",
    "The teaches table links event to staff.\n",
    "\n",
    "- ut_occurs\n",
    "\n",
    "event | week\n",
    "------|-------:\n",
    "co12004.L01 | 01\n",
    "co12004.L01 | 02\n",
    "co12004.L01 | 03\n",
    "co12004.L01 | 04\n",
    "co12004.L01 | 05\n",
    " | ... Result truncated.\n",
    "\n",
    "Most events occur on weeks 01 to 13. This table records exactly which weeks are part of the plan for each event.\n",
    "\n",
    "- ut_modle\n",
    "\n",
    "id | name\n",
    "---|---------\n",
    "co12001 | Rapid Application Development\n",
    "co12002 | Software Development 1A\n",
    "co12003 | Professional Skills\n",
    "co12004 | Applications Workshop\n",
    " | ... Result truncated.\n",
    "\n",
    "By the 21st century, the Gradgrindians had completed their assimilation of Education. The process of \"modularisation\" was just one of their crimes against learning.\n",
    "\n",
    "- ut_week\n",
    "\n",
    "id | wkstart\n",
    "--:|-------:\n",
    "01 | 02/10/00\n",
    "02 | 09/10/00\n",
    "03 | 16/10/00\n",
    " | ...Result truncated\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prerequesites\n",
    "from pyhive import hive\n",
    "%load_ext sql\n",
    "%sql hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "**Give the room id in which the event co42010.L01 takes place.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>room</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>cr.132</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('cr.132',)]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT room FROM ut_event\n",
    "  WHERE id='co42010.L01'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "**For each event in module co72010 show the day, the time and the place.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>dow</th>\n",
       "        <th>tod</th>\n",
       "        <th>room</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Wednesday</td>\n",
       "        <td>14:00</td>\n",
       "        <td>cr.SMH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Tuesday</td>\n",
       "        <td>09:00</td>\n",
       "        <td>cr.B8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Wednesday</td>\n",
       "        <td>09:00</td>\n",
       "        <td>co.B7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Tuesday</td>\n",
       "        <td>12:00</td>\n",
       "        <td>co.LB42+LB46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Tuesday</td>\n",
       "        <td>11:00</td>\n",
       "        <td>co.G75+G76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Wednesday</td>\n",
       "        <td>16:00</td>\n",
       "        <td>co.LB42+LB46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Thursday</td>\n",
       "        <td>10:00</td>\n",
       "        <td>co.LB42+LB46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Wednesday</td>\n",
       "        <td>13:00</td>\n",
       "        <td>co.117+118</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Wednesday', '14:00', 'cr.SMH'),\n",
       " ('Tuesday', '09:00', 'cr.B8'),\n",
       " ('Wednesday', '09:00', 'co.B7'),\n",
       " ('Tuesday', '12:00', 'co.LB42+LB46'),\n",
       " ('Tuesday', '11:00', 'co.G75+G76'),\n",
       " ('Wednesday', '16:00', 'co.LB42+LB46'),\n",
       " ('Thursday', '10:00', 'co.LB42+LB46'),\n",
       " ('Wednesday', '13:00', 'co.117+118')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT dow, tod, room FROM ut_event \n",
    "    WHERE modle='co72010'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "**List the names of the staff who teach on module co72010.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Chisholm, Ken</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Cumming, Andrew</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Chisholm, Ken',), ('Cumming, Andrew',)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT name\n",
    "  FROM ut_teaches JOIN ut_event ON (\n",
    "    ut_teaches.event=ut_event.id) JOIN\n",
    "    ut_staff ON (ut_staff.id=ut_teaches.staff)\n",
    "    WHERE modle='co72010'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "**Give a list of the staff and module number associated with events using room cr.132 on Wednesday, include the time each event starts.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>modle</th>\n",
       "        <th>tod</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Murray, Jim</td>\n",
       "        <td>co22009</td>\n",
       "        <td>12:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Varey, Alison</td>\n",
       "        <td>co32021</td>\n",
       "        <td>09:00</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Murray, Jim', 'co22009', '12:00'), ('Varey, Alison', 'co32021', '09:00')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT name, modle, tod FROM\n",
    "  ut_event JOIN ut_teaches ON (ut_event.id=ut_teaches.event) JOIN\n",
    "    ut_staff ON (ut_teaches.staff=ut_staff.id)\n",
    "    WHERE room='cr.132' AND dow='Wednesday'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "**Give a list of the student groups which take modules with the word 'Database' in the name.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>BEng4 Network and Distributing Systems</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>BSc4 Computing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD IT and e-Commerce</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD IT and e-Commerce eve sem 3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information System pt. Tues</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information Systems</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information Systems a (HCI)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information Systems b (DS)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information Systems d (BT)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Information Systems e (OOP)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Software Engineering</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Software Technology</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>PgD Software Technology pt. Tues</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(None,),\n",
       " ('BEng4 Network and Distributing Systems',),\n",
       " ('BSc4 Computing',),\n",
       " ('PgD IT and e-Commerce',),\n",
       " ('PgD IT and e-Commerce eve sem 3',),\n",
       " ('PgD Information System pt. Tues',),\n",
       " ('PgD Information Systems',),\n",
       " ('PgD Information Systems a (HCI)',),\n",
       " ('PgD Information Systems b (DS)',),\n",
       " ('PgD Information Systems d (BT)',),\n",
       " ('PgD Information Systems e (OOP)',),\n",
       " ('PgD Software Engineering',),\n",
       " ('PgD Software Technology',),\n",
       " ('PgD Software Technology pt. Tues',)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT ut_student.name\n",
    "  FROM ut_student JOIN ut_attends ON (\n",
    "      ut_student.id=ut_attends.student) JOIN\n",
    "    ut_event ON (ut_attends.event=ut_event.id) JOIN\n",
    "    ut_modle ON (ut_event.modle=ut_modle.id)\n",
    "    WHERE LOWER(ut_modle.name) LIKE '%database%'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
